
local serviceId
local conns = {}

function OnInit(id)
    print("[lua] chat OnInit["..id.."]")
    serviceId = id
    sunnet.Listen(8002, id);
end

function OnAcceptMsg(listenfd, clientfd)
    print("[lua] chat OnAcceptMsg["..serviceId.."] listenfd:"..listenfd.." clientfd:"..clientfd)
    conns[clientfd] = true
end

function OnSocketData(fd, buff)
    print("[lua] chat OnSocketData["..serviceId.."] fd:"..fd)
    for id,_ in pairs(conns) do
        sunnet.Write(id, buff)
    end
end

function OnSocketClose(fd)
    print("[lua] chat OnSocketClose["..serviceId.."] fd:"..fd)
    conns[fd] = nil
end

function OnExit()
    print("[lua] chat["..serviceId.."] OnExit")
end